package com.js.basic.controller;

import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.vo.BasicCode;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.js.basic.domain.CodeRules;
import com.js.basic.service.ICodeRulesService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 编码规则Controller
 *
 * @author lzq
 * @date 2024-06-14
 */
@RestController
@RequestMapping("/rules")
public class    CodeRulesController extends BaseController {
    @Autowired
    private ICodeRulesService codeRulesService;


    /**
     * 查询编码规则列表
     */
    @RequiresPermissions("basic:rules:list")
    @GetMapping("/list")
    public TableDataInfo list(CodeRules codeRules) {
        startPage();
        List<CodeRules> list = codeRulesService.selectCodeRulesList(codeRules);
        return getDataTable(list);
    }

    /**
     * 导出编码规则列表
     */
    @RequiresPermissions("basic:rules:export")
    @Log(title = "编码规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CodeRules codeRules) {
        List<CodeRules> list = codeRulesService.selectCodeRulesList(codeRules);
        ExcelUtil<CodeRules> util = new ExcelUtil<CodeRules>(CodeRules.class);
        util.exportExcel(response, list, "编码规则数据");
    }

    /**
     * 获取编码规则详细信息
     */
    @RequiresPermissions("basic:rules:query")
    @GetMapping(value = "/{encoId}")
    public AjaxResult getInfo(@PathVariable("encoId") Long encoId) {
        return success(codeRulesService.selectCodeRulesByEncoId(encoId));
    }

    /**
     * 新增编码规则
     */
    @RequiresPermissions("basic:rules:add")
    @Log(title = "编码规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CodeRules codeRules) {
        try {
            BasicCode basicCode = DateUtils.getBasicCode(codeRules.getDateRule(), codeRules.getSerialNumber());

            codeRules.setEncodeRule(basicCode.getCode_format());
            codeRules.setEncodeNow(codeRules.getTableCode() + basicCode.getTime_format() + basicCode.getIndex());
            return toAjax(codeRulesService.insertCodeRules(codeRules));
        } catch (DuplicateKeyException e) {
            // 处理唯一约束错误
            return AjaxResult.error(0, "新增失败");
        }


    }

    /**
     * 修改编码规则
     */
    @RequiresPermissions("basic:rules:edit")
    @Log(title = "编码规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CodeRules codeRules) {
        BasicCode basicCode = DateUtils.getBasicCode(codeRules.getDateRule(), codeRules.getSerialNumber());

        codeRules.setEncodeRule(basicCode.getCode_format());
        codeRules.setEncodeNow(codeRules.getTableCode() + basicCode.getTime_format() + basicCode.getIndex());
        return toAjax(codeRulesService.updateCodeRules(codeRules));
    }

    /**
     * 删除编码规则
     */
    @RequiresPermissions("basic:rules:remove")
    @Log(title = "编码规则", businessType = BusinessType.DELETE)
    @DeleteMapping("/{encoIds}")
    public AjaxResult remove(@PathVariable Long[] encoIds) {
        return toAjax(codeRulesService.deleteCodeRulesByEncoIds(encoIds));
    }


    /**x
     * 通过表名查找该表编码规则
     */
    @GetMapping("/getTableCode/{tableName}")
    public CodeRules getCodeByTableName(@PathVariable("tableName") String tableName) {
        return codeRulesService.selectCodeRulesByTableName(tableName);
    }


    @PostMapping("/updateCodeNow")
    public AjaxResult updateCodeNowByTableName(@RequestBody CodeRules codeRules) {
        return toAjax(codeRulesService.updateCodeNowByTableName(codeRules));
    }

}
